﻿using System.Collections.Generic;
using System.Linq;
using System;
using SDT.DataService.Contexts;
using SDT.DataService.Repositories;
using SDT.DataService.Services;
using SDT.Libraries.Utilities;

namespace SDT.DataService.Services
{
   public class Spider_SiteService : IService<Spider_Site>
    {
        #region Field
        
        private IRepository<Spider_Site> repository;
        

        #endregion

        #region Constructor
        public Spider_SiteService()
            : this(new Repository<Spider_Site>())
        {
        }

        public Spider_SiteService(IRepository<Spider_Site> repository)
        {
            this.repository = repository;
          
        }
        #endregion

        #region Method
        public int InsertChannel(string name)
        {
            Spider_SiteContext contextObj = new Spider_SiteContext();
            try
            {
                return contextObj.InsertChannel(name);
            }
            catch (Exception ex)
            {
                new LogException().Write(string.Format("  Module {0} --Msg {1} ", "Spider_StieService.InsertChannel", ex.Message));

            }
            finally { contextObj.Dispose(); }
            return 0;
        
        }
        public int UpdateChannel(int cid, string name)
        {
            Spider_SiteContext contextObj = new Spider_SiteContext();
            try
            {
                return contextObj.UpdateChannel(cid, name);
            }
            catch (Exception ex)
            {
               new LogException().Write(string.Format("  Module {0} --Msg {1} ", "Spider_StieService.UpdateChannel", ex.Message));

            }
            finally { contextObj.Dispose(); }
            return 0;
        }


        public void BuildSiteTree(List<Spider_Site> catList, int nParentID, int nLevel)
        {
          
        }
        #endregion

        #region IService<Spider_Site> Members
        public IEnumerable<Spider_Site> FillAll(Func<Spider_Site, bool> exp)
        {
            try
            {
                return GetAll().Where<Spider_Site>(exp);
            }
            catch (Exception ex)
            {
                return null;

            }
        }


        public IQueryable<Spider_Site> GetAll()
        {
            try
            {
                return repository.GetAll();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool Update(Spider_Site entity)
        {
            try
            {
                repository.Update(entity);
                return true;
            }
            catch
            {
                return false;

            }
        }

        public bool Create(Spider_Site entity)
        {
            try
            {
                //entity.CreatedDate = DateTime.Now;
                repository.Create(entity);
                return true;
            }
            catch (Exception ex)
            {

                return false;

            }
        }

        public bool Delete(Spider_Site entity)
        {
            try
            {
               // entity.isDeleted = true;
                return Update(entity);
            }
            catch (Exception ex)
            {

                return false;
            }

        }

        public IQueryable<Spider_Site> GetAll(bool isDeleted, int parentId)
        {
            try
            {
                return repository.GetAll();
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        #endregion

        #region IService<Spider_Site> Members

       

        #endregion
    }
}
